﻿using System;
using System.Collections.Generic;
using Cysharp.Threading.Tasks;
using IQIGame.Onigao.Framework;

namespace IQIGame.Onigao.GamePlay
{
    public class BattleRenderTurnController : BattleRenderBaseController
    {
        // 当前回合已经执行过的turn的Unit
        public List<UnitView> beforeQueue = new List<UnitView>();

        // 当前回合仍在队列的的turn的Unit
        public List<UnitView> afterQueue = new List<UnitView>();

        public UnitView current;
        public bool turnFirstPart;
        
        public void SyncAfterTurn(List<int> queue, bool bRoundStart)
        {
            if (bRoundStart)
            {
                this.current = null;
                this.ClearBefore();
            }

            this.beforeQueue.RemoveAll((x) => queue.Contains(x.GUID));
            this.afterQueue.Clear();
            for (int i = 0; i < queue.Count; i++)
            {
                var nUID = queue[i];
                if (nUID != this.current?.GUID)
                {
                    var rUnit = this.mCtrl.data.GetUnit(nUID);
                    if (rUnit != null)
                    {
                        this.afterQueue.Add(rUnit);
                    }
                }
            }
            
            MsgDispatcher.Broadcast(MsgEventType.BattleView_TurnQueueChange);
        }


        public void SetCurrent(long uid)
        {
            if (this.current != null)
            {
                this.beforeQueue.Add(this.current);
            }
            this.current = this.mCtrl.data.GetUnit(uid);
        }

        private void ClearBefore()
        {
            this.beforeQueue.Clear();
        }
    }
}